package br.ucb.cesbdao.model.impl;

import java.sql.SQLException;
import java.text.DecimalFormat;

import lombok.Getter;
import lombok.Setter;

import org.apache.log4j.Logger;

import br.ucb.cesbdao.enuns.EnumMensagens;
import br.ucb.cesbdao.model.NaoAgendadoModel;
import br.ucb.cesbdao.persistencia.dao.NaoAgendadoDAO;
import br.ucb.cesbdao.persistencia.factory.FactoryConnection;
import br.ucb.cesbdao.persistencia.factory.FactoryDAO;
import br.ucb.cesbdao.servicos.Servicos;
import br.ucb.cesbdao.visao.Visao;


public class NaoAgendadoModelImpl implements NaoAgendadoModel{

	private Logger logger = Servicos.carregaLog(NaoAgendadoModelImpl.class);
	@Getter @Setter
	private NaoAgendadoDAO dao;
	
	public NaoAgendadoModelImpl() {
	}

	@Override
	public int getToday() {
		try{
			setDao(FactoryDAO.getDaoNaoAgendado(FactoryConnection.getInstance().criaConexao()));
			return getDao().getToday();
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return 0;
	}

	@Override
	public void add() {
		try{
			setDao(FactoryDAO.getDaoNaoAgendado(FactoryConnection.getInstance().criaConexao()));
			getDao().add();
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
	}

	@Override
	public void update(int qnt) {
		try{
			setDao(FactoryDAO.getDaoNaoAgendado(FactoryConnection.getInstance().criaConexao()));
			getDao().update(qnt);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
	}

	@Override
	public void dell() {
		try{
			setDao(FactoryDAO.getDaoNaoAgendado(FactoryConnection.getInstance().criaConexao()));
			getDao().dell();
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
	}
	
	@Override
	public String qtdNaoAgendado(){
		DecimalFormat dc = new DecimalFormat("00");
		try{
			setDao(FactoryDAO.getDaoNaoAgendado(FactoryConnection.getInstance().criaConexao()));
			return (String.valueOf(dc.format(getDao().getToday())));
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return null;
	}
	
	@Override
	public String qtdNaoAgendados() {
		if(Visao.showYesNo(EnumMensagens.MSG_Q_CONFIRMACAO_N_AGENDADO.getMensagem(), "Confirmação")){
			try{
				setDao(FactoryDAO.getDaoNaoAgendado(FactoryConnection.getInstance().criaConexao()));
				getDao().add();
				return qtdNaoAgendado();
			} catch (ClassNotFoundException | SQLException e) {
				logger.error(e.getMessage());
				e.printStackTrace();
			} finally {
				FactoryConnection.getInstance().closeConexao(getDao().getConexao());
			}
		}
		return null;
	}
	@Override
	public String retirarQtdNaoAgendados(String texto) {
		if(Visao.showYesNo(EnumMensagens.MSG_Q_CONFIRMACAO_REMOVER_N_AGENDADO.getMensagem(), "Confirmação")){
			try{
				setDao(FactoryDAO.getDaoNaoAgendado(FactoryConnection.getInstance().criaConexao()));
				if(texto.equalsIgnoreCase("00")){
					Visao.showMessageError(EnumMensagens.MSG_ERRO_R_REGISTRO.getMensagem(),"Erro");
					return null;
				}
				getDao().dell();
				return qtdNaoAgendado();
			} catch (ClassNotFoundException | SQLException e) {
				logger.error(e.getMessage());
				e.printStackTrace();
			} finally {
				FactoryConnection.getInstance().closeConexao(getDao().getConexao());
			}
		}
		return null;
	}

	@Override
	public void updateNaoAgendados(Integer qnt) {
		try{
			setDao(FactoryDAO.getDaoNaoAgendado(FactoryConnection.getInstance().criaConexao()));
			getDao().update(qnt);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
	}
}
